<template>
<div>
  <el-tree :data="menus" :props="defaultProps" @node-click="handleNodeClick" :expand-on-click-node="false" show-checkbox node-key="catId" :default-expanded-keys="expandedKeys">
    <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span v-if="node.level === 3">{{data.productCount}}</span>
        <span>
          <el-button
            type="text"
            size="mini"
            v-if="node.level === 3"
            @click="() => updateQuantity(node, data)">
            修改数量
          </el-button>
        </span>
      </span>
  </el-tree>


  <el-dialog
    title="提示"
    :visible.sync="updateVisible"
    width="30%"
    :before-close="handleClose">
    <el-form :model="category">
      <el-form-item label="修改数量">
        <el-input v-model="category.productCount" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
    <el-button @click="updateVisible = false">取 消</el-button>
    <el-button type="primary" @click="updateAction()">确 定</el-button>
  </span>
  </el-dialog>
</div>
</template>


<script>
export default {
  data () {
    return {
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      expandedKeys: [],
      menus: [],
      updateVisible: false,
      category: {
        name: '',
        parentCid: 0,
        catLevel: 0,
        showStatus: 1,
        sort: 0,
        productCount: 0
      }
    }
  },
  methods: {
    handleNodeClick (data) {
      console.log(data)
    },
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {
        })
    },
    updateQuantity (node, data) {
      this.category = data
      this.updateVisible = true
    },
    getMenu () {
      this.$http({
        url: this.$http.adornUrl('/product/category/list/tree'),
        method: 'get'
      }).then((response) => {
        console.log('Raw response:', response)
        if (response && response.data) {
          this.menus = response.data.data
        } else {
          console.error('Invalid response structure:', response)
        }
      }).catch((error) => {
        console.error('Request failed:', error)
      })
    },
    updateAction () {
      this.$http({
        url: this.$http.adornUrl('/product/category/update'),
        method: 'post',
        data: this.$http.adornData(this.category, false)
      }).then(response => {
        this.$message({
          type: 'success',
          message: '修改成功'
        })
      }).catch((error) => {
        console.error('Request failed:', error)
        this.$message({
          type: 'info',
          message: '修改失败'
        })
        this.getMenu()
      })
      this.updateVisible = false
      // 设置需要默认展开的菜单
      this.expandedKeys = [this.category.parentCid]
      this.category = {
        name: '',
        parentCid: 0,
        catLevel: 0,
        showStatus: 1,
        sort: 0,
        productCount: 0
      }
    }
  },
  created () {
    this.getMenu()
  }
}
</script>


<style scoped lang="scss">

</style>
